/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <pcap.h>
#include <remote-ext.h>

#include "MIpHelper.h"
#include "MIpInterfaceInfo.h"

#define MAX_MIP_INTERFACE_INFO 10

class MWinPcap
{
private:

	char errbuf[PCAP_ERRBUF_SIZE];

	CString friendlyName;

	CString intfName;
	bool GetIntfDevice(short);
		bool FindAddressFamily(pcap_addr*, short);

	pcap_t *captureHandle;

	int intfInfoCnt;
	MIpInterfaceInfo intfInfo[MAX_MIP_INTERFACE_INFO];
	void PopulateInterfaceInfo(pcap_if_t*, short);

public:
	MWinPcap(void);
	~MWinPcap(void);

	MIpHelper ipHelper;

	CString defaultInterfaceName;

	DWORD   GetLocalIP(void);
	DWORD   GetLocalIPMask(void);
	DWORD   GetLocalIPBroadcastAddress(void);
	bool    GetIpInterface(void);
	CString GetIpInterfaceName(void);
	CString GetIpInterfaceAdapterName(void);
	int     GetIpInterfaceCount(void);
	MIpInterfaceInfo* GetIpInterfaceInfo(int);
	DWORD   GetGatewayIP(void);
	DWORD   GetDnsServerIP(void);
	bool    IpAddrBelongsToLocalNet(DWORD);

	bool BeginCapture(int);
	bool BeginCapture(void);
	int CaptureOnePacket(BYTE*, int*);
		// return values :
		//		-3 = error
		//		-2 = fragmented packet
		//		-1 = buffer too small
		//		 0 = time-out
		//		 1 = OK
	void EndCapture(void);

	void FillSrcMAC(BYTE*);
	void FillSrcIp(BYTE*);

	void FillDnsMAC(BYTE*);
	void SetDnsMAC(BYTE*);

	void GetIPv6UnicastAddress(in_addr6*);
	void GetIPv6AnycastAddress(in_addr6*);
	void GetIPv6MulticastAddress(in_addr6*);
	void GetIPv6DNSAddress(in_addr6*);

	bool SendPacket(BYTE*, int);
};
